<%args>
  $select_all       => ''
  $app_save         => ''
  $app_delete       => ''
  $app_dup          => ''
  $list_ns          => ''

  $cd_action        => ''
  $cd_audit         => ''
  $cd_cache_expire1 => ''
  $cd_cache_expire2 => ''
  $cd_cache         => ''
  $cd_id            => ''
  $cd_name          => ''
  $cd_namespace     => ''
  $cd_profile       => ''
  $cd_repl          => ''
  $cd_return        => ''
  $cd_stmt          => ''
  $cd_type          => ''
  $cd_version       => ''

  $chk_prod         => ''
  $chk_roll         => ''
  $chk_dev          => ''
</%args>
<%attr>
  a_content => '2col'
  a_module => 'admin'
</%attr>
<%method onload>
onLoad="initDioTable();"
</%method>
<%init>
  # store namespace in a cookie
  my %cookies = parse_raw LsL::MyCookie($r->headers_in->{Cookie});

  #$list_ns = $list_ns || exists $cookies{'towzer.dio_ns'} && $cookies{'towzer.dio_ns'} ? $cookies{'towzer.dio_ns'}->value : '';

  my ($cache_expire1, $cache_expire2) = ('','');

  # submit checks
  if ($app_save) {
    my ($exp1,$cache_exp, $sysclass, $profile);
    $profile = '1' if $cd_profile;
    $sysclass = 'production' if $chk_prod;
    $sysclass .= ($sysclass eq ""?"":",") . 'rollout' if $chk_roll;
    $sysclass .= ($sysclass eq ""?"":",") . 'dev' if $chk_dev;
    $exp1 = $cd_cache_expire1 || '';
    $exp1 =~ s/[^0-9]//g;
    $cache_exp = $exp1 . ' ' . $cd_cache_expire2;
    $cache_exp =~ s/^\ //;
    if ($cd_id) {
      LsL::D_DIO::dio_update({cd_id =>           $cd_id,
                               cd_namespace =>    $cd_namespace,
                               cd_name =>         $cd_name,
                               cd_version =>      $cd_version,
                               cd_sysclass =>     $sysclass,
                               cd_type =>         $cd_type,
                               cd_return =>       $cd_return,
                               cd_cache =>        $cd_cache,
                               cd_cache_expire => $cache_exp,
                               cd_stmt =>         $cd_stmt,
                               cd_stmt_noarg =>   '',
                               cd_profile =>      $profile,
                               cd_repl =>         $cd_repl,
                               cd_action =>       $cd_action,
                               cd_audit =>        $cd_audit,
                             });
    } else {
      LsL::D_DIO::dio_insert({cd_id =>           $cd_id,
                               cd_namespace =>    $cd_namespace,
                               cd_name =>         $cd_name,
                               cd_version =>      $cd_version,
                               cd_sysclass =>     $sysclass,
                               cd_type =>         $cd_type,
                               cd_return =>       $cd_return,
                               cd_cache =>        $cd_cache,
                               cd_cache_expire => $cache_exp,
                               cd_stmt =>         $cd_stmt,
                               cd_stmt_noarg =>   '',
                               cd_profile =>      $profile,
                               cd_repl =>         $cd_repl,
                               cd_action =>       $cd_action,
                               cd_audit =>        $cd_audit,
                             });
    }
    $m->comp('/lib/refresh_box', 'title' => 'DIO Saved...', 'path' => '/admin/lab/dio/edit.html', 'query' => { 'cd_id' => $cd_id } );
    return;
  } elsif ($app_dup) {
    my $new_id = 0;
    $new_id = LsL::D_DIO::dio_duplicate({cd_id => $cd_id});
    $m->comp('/lib/refresh_box', 'title' => 'DIO Duplicated...', 'path' => '/admin/lab/dio/edit.html', 'query' => { 'cd_id' => $new_id } );
    return;
  } elsif ($app_delete) {
    my $new_id = 0;
    LsL::D_DIO::dio_delete({cd_id => $cd_id});
    $m->comp('/lib/refresh_box', 'title' => 'DIO Deleted...', 'path' => '/admin/lab/dio/edit.html', 'query' => { } );
    return;
  }


  # grab name spaces
  my $ns = LsL::D_DIO::get_namespaces();
  my $namespace_values;
  my $namespace_labels;
  $namespace_labels->{''} = '';
  my $opt_str = '<option ' . ($list_ns eq 'All'?'selected':'') . ' value="All">All</option>';
  foreach my $n (@$ns) {
    push @$namespace_values, $n->[0];
    $namespace_labels->{$n->[0]} = $n->[0];
    $opt_str .= q{<option } . ($list_ns eq $n->[0] ?'selected':'') . q{ value="} . $n->[0] . q{">} . $n->[0] . q{</option>};
  }

  # grab dio list
  my $items = new LsL::D_DIO::dio_list();

  # grab dio
  my $dio      = {};
  my $in_keys  = {};
  my $out_keys = {};
  my $exp_keys = {};
  my %key_hash = ();

  if ($cd_id) {
    $dio      = LsL::D_DIO::get_dio( { 'cd_id' => $cd_id } );
    $in_keys  = LsL::D_DIO::get_dio_inkeys( { 'cd_id' => $cd_id } );
    $out_keys = LsL::D_DIO::get_dio_outkeys( { 'cd_id' => $cd_id } );
    $exp_keys = LsL::D_DIO::get_dio_exps( { 'cd_id' => $cd_id } );

    # build keys lister hash
    my $c = 0;
    foreach my $key (@$in_keys) {
      push @{ $key_hash{$c++}{'i'} }, $key->{'ci_name'},
                               $key->{'ci_pos'},
                               $key->{'ci_req'},
                               $key->{'ci_default'};
    }
    $c = 0;
    foreach my $key (@$out_keys) {
      push @{ $key_hash{$c++}{'o'} }, $key->{'co_name'},
                               $key->{'co_pos'},
                               $key->{'co_default'};
    }
    $c = 0;
    foreach my $key (@$exp_keys) {
      push @{ $key_hash{$c++}{'e'} }, $key->{'ce_name'};
    }

    ($cache_expire1, $cache_expire2) = split(' ',
       $dio->{'cd_cache_expire'});
    if ($cache_expire2 eq '') {
      $cache_expire1 = '';
      $cache_expire2 = 'never';
    };
  }
  # list box title
  my $list_title_right = q{<b>Namespace:</b>&nbsp;<select name="list_ns" onchange="toggleVis(this.options[this.selectedIndex].value)">} . $opt_str . q{</select>&nbsp;<b>Action:</b>&nbsp;<select name="b_action"><option value="0">Select All<option value="1">Enable Profile on Selected<option value="2">Disable Profile on Selected<option value="3">Reset Profile Data on Selected<option value="4">Reset ALL Profile Data</select>&nbsp;&nbsp;<input type="submit" name="app_b_go" type="button" value="Go">&nbsp;};
</%init>
<script type="text/javascript">
<!--
function set_ns_cookie( form )
{
if( form == null )
return false;
var ns = form.elements[ "cd_namespace" ];
// write login cookies
writeCookie("para_ns", ns.value,30 );
// alert( pd );
return true;
}
// -->
</script>
<form method="post" name="d_form" action="/admin/lab/dio/edit.html">
<div class="grid_5">
<&| /lib/box-fs, 'class' => "input", 'title' => 'DIO Editor' &>
<input type="hidden" name="cd_id" value="<% $cd_id %>" />
<table width="100%" class="no_color" border="0" cellpadding="4" cellspacing="0">
<tr>
<td align="left" colspan="2" nowrap>
% if ($cd_id) {
<input class="button" type="button" name="app_test" value="Test Harness" onclick="location.replace('<& /lib/url, path => '/lab/dio/test', query => {dio_id => $cd_id} &>');" />&nbsp;
<input class="button" type="submit" name="app_delete" value="Delete" />&nbsp;
<input class="button" type="submit" name="app_dup" value="Duplicate" />&nbsp;
% }
<input class="button" type="button" name="app_clear" value="Clear" onclick="location.replace('/admin/lab/dio/edit.html');") />&nbsp;
<input class="button" type="submit" name="app_save" value="Save" />&nbsp;
</td>
</tr>
<tr class="header">
<td align="left" colspan="2">General</td>
</tr>
<tr class="lt">
<td align="left" valign="middle" width="1"><nobr>Current Namespaces:</nobr><br></td>
<td align="left" valign="middle">
<& /lib/dropdown, 'name' => "c_namespace", 'values' => $namespace_values, 'labels' => $namespace_labels, 'value' => $dio->{'cd_namespace'}, 'onchange' => 'set_type(this)' &>
</td>
</tr>
<tr class="dk">
<td align="left" valign="middle" width="1"><nobr>Namespaces:</nobr><br></td>
<td align="left" valign="middle">
<input type="text" id="cd_namespace" name="cd_namespace" size="40" value="<% $dio->{'cd_namespace'} %>" />
</td>
</tr>
<tr class="lt">
<td align="left" valign="middle" width="1"><nobr>Name:</nobr><br></td>
<td colspan="3" align="left" valign="middle">
<input type="text" name="cd_name" size="40" value="<% $dio->{'cd_name'} %>" />
</td>
</tr>
<tr class="dk">
<td align="left" valign="middle" width="1"><nobr>Version:</nobr><br></td>
<td align="left" valign="middle">
<input type="text" name="cd_version" size="5" value="<% $dio->{'cd_version'} %>" />
</td>
</tr>
<tr class="lt">
<td align="left" valign="middle" width="1"><nobr>Return:</nobr><br></td>
<td align="left" valign="middle">
<& /lib/dropdown, 'name' => 'cd_return', 'values' => \@LAB::SysDIO::return_values, 'labels' => \%LAB::SysDIO::return_labels, 'value' => $dio->{'cd_return'} &>
</td>
</tr>
<tr class="lt">
<td align="left" valign="middle" width="1"><nobr>System Classes:</nobr><br></td>
<td align="left" valign="middle"><input <% ($dio->{'cd_sysclass'} =~ /production/?'checked':'') %> type="checkbox" name="chk_prod">&nbsp;Production&nbsp;<input <% ($dio->{'cd_sysclass'} =~ /rollout/?'checked':'') %> type="checkbox" name="chk_roll">&nbsp;Rollout&nbsp;<input <% ($dio->{'cd_sysclass'} =~ /dev/?'checked':'') %> type="checkbox" name="chk_dev">&nbsp;Development</td>
</tr>
<tr class="dk">
<td align="left" valign="middle" width="1"><nobr>Type:</nobr><br></td>
<td align="left" valign="middle">
<& /lib/dropdown, 'name' => 'cd_type', 'values' => \@LAB::SysDIO::type_values, 'labels' => \%LAB::SysDIO::type_labels, 'value' => $dio->{'cd_type'} &>
</td>
</tr>
<tr class="lt">
<td align="left" valign="middle" width="1"><nobr>Cache:</nobr><br></td>
<td align="left" valign="middle">
<& /lib/dropdown, 'name' => 'cd_cache', 'values' => \@LAB::SysDIO::cache_type_values, 'labels' => \%LAB::SysDIO::cache_type_labels, 'value' => substr($dio->{'cd_cache'},0,1) &>
</td>
</tr>
<tr class="lt">
<td align="left" valign="middle" width="1"><nobr>Cache Expire:</nobr><br></td>
<td align="left" valign="middle">
<input style="vertical-align: middle;" type="text" name="cd_cache_expire1" size="10" value="<% $cache_expire1 %>" />&nbsp;<& /lib/dropdown, 'name' => 'cd_cache_expire', 'values' => \@LAB::SysDIO::cache_exp_values, 'labels' => \%LAB::SysDIO::cache_exp_labels, 'value' => $cd_cache_expire2 &>
</td>
<tr class="dk">
<td align="left" valign="middle" width="1"><nobr>Profile:</nobr><br></td>
<td align="left" valign="middle">
<& /lib/dropdown, 'name' => 'cd_profile', 'values' => ['',1], 'labels' => {'' => '', 1 => 'Yes'}, 'value' => $dio->{'cd_profile'} &>
</td>
</tr>
<tr class="dk">
<td align="left" valign="middle" width="1"><nobr>Audit:</nobr><br></td>
<td colspan="3" align="left" valign="middle">
<& /lib/dropdown, 'name' => 'cd_audit', 'values' => \@LAB::SysDIO::audit_type_values, 'labels' => \%LAB::SysDIO::audit_type_labels, 'value' => $dio->{'cd_audit'} &>
</td>
</tr>
<tr class="dk">
<td align="left" valign="middle" width="1"><nobr>Replication:</nobr><br></td>
<td align="left" valign="middle">
<& /lib/dropdown, 'name' => 'cd_repl', 'values' => \@LAB::SysDIO::repl_values, 'labels' => \%LAB::SysDIO::repl_labels, 'value' => $dio->{'cd_repl'} &>
</td>
</tr>
<tr class="lt">
<td align="left" valign="middle" width="1"><nobr>Action:</nobr><br></td>
<td align="left" valign="middle">
<& /lib/dropdown, 'name' => 'cd_action', 'values' => \@LAB::SysDIO::action_values, 'labels' => \%LAB::SysDIO::action_labels, 'value' => $dio->{'cd_action'} &>
</td>
</tr>
<tr class="dk"
<td colspan="2" align="left" valign="middle" width="1"><nobr>Statement:</nobr><br></td>
</tr>
<tr class="lt">
<td colspan="2" align="left" valign="top" style="vertical-align:top;">
<textarea name="cd_stmt" rows="8" cols="50" wrap="soft"><% $dio->{'cd_stmt'} |h %></textarea>
</td>
</tr>
</table>
% if ($cd_id) {
<table class="lt_blue" border="0" cellpadding="4" cellspacing="0" width="100%">
<tr class="header">
<td colspan="2" align="left"><a href="<& /lib/url, 'path' => '/admin/lab/dio/inkey.html', 'query' => {'cd_id' => $cd_id, 'list_ns' => $list_ns, 'cd_name' => $dio->{'cd_name'}} &>">In Keys</a></td>
<td colspan="2" align="left"><a href="<& /lib/url, 'path' => '/admin/lab/dio/outkey.html', 'query' => {'cd_id' => $cd_id, 'list_ns' => $list_ns, 'cd_name' => $dio->{'cd_name'}} &>">Out Keys</a></td>
</tr>
<tr class="dk">
<td align="left"><b>Name</b></td>
<td align="left"><b>Position</b></td>
<td align="left"><b>Name</b></td>
<td align="left"><b>Position</b></td>
</tr>
%   my $rcnt = 0;
%   foreach my $k (sort {$a <=> $b} keys %key_hash) {
<tr class="<% ($rcnt++ % 2 ? 'dk' : 'lt') %>">
<td align="left"><% @{$key_hash{$k}{'i'}}[0] %></td>
<td align="left"><% @{$key_hash{$k}{'i'}}[1] %></td>
<td align="left"><% @{$key_hash{$k}{'o'}}[0] %></td>
<td align="left"><% @{$key_hash{$k}{'o'}}[1] %></td>
</tr>
%   }
</table>
% }
</&>
</div>
<div class="grid_5">
<&| /lib/box-fs, 'title' => 'DIO List', 'title_right' => $list_title_right, btype => 5 &>
<table class="lt_blue" name="DioTable" id="DioTable" border="0" cellpadding="4" cellspacing="0" width="100%">
<tr class="header" name="_header" id="_header">
<td align="center" nowrap><b>&nbsp;</b></td>
<td align="left" nowrap><b>&nbsp;Namespace&nbsp;</b></td>
<td align="center" nowrap><b>&nbsp;Name&nbsp;</b></td>
<td align="center" nowrap><b>&nbsp;Type</b></td>
<td align="center" nowrap><b>&nbsp;Action</b></td>
</tr>
% my $nc = '';
% my $z = 0;
% my $it = 0;
% while(my $i = $items->iter()) {
%   if ($i->{'cd_namespace'} ne $nc) {
%     $it++ if $nc ne '';
%     $nc = $i->{'cd_namespace'};
%     $z  = 1;
%   } else {
%     $z = 0;
%   }
%   my $rc = $it % 2 ? "dk" : "lt";
%   my $t = 'cgi->param("chk_' . $i->{'cd_id'} . '")';
<tr name="<% $i->{'cd_namespace'} %>" class="<% $rc %>" id="<% $i->{'cd_namespace'} %>">
<td align="center" nowrap>
<input type="checkbox" <% (($select_all || eval $t) ? 'checked':'') %> name="chk_<% $i->{'cd_id'} %>"></td>
<td align="left" nowrap>&nbsp;<% ($z?$nc:'') %>&nbsp;</td>
<td align="left" nowrap>&nbsp;<a href="<& /lib/url, 'path' => '/admin/lab/dio/edit.html', 'query' => {'list_ns' => $i->{'cd_namespace'}, 'cd_id' => $i->{'cd_id'}} &>"><% $i->{'cd_name'} %></a></td>
<td align="left" nowrap>&nbsp;<% $i->{'cd_type'} %>&nbsp;</td>
<td align="left" nowrap>&nbsp;<% $i->{'cd_action'} %>&nbsp;</td>
</tr>
% }
</table>
</&>
</div>
</form>
